//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

#define THREADAMOUNT        100

EXTERN void DzYield();

DzMutex       g_startMutex;
SystemTime      g_startTime;

Int32 StartTimeThread(void *pvPara)
{
    IThread         *pICurrentThread;
    SystemTime          subStartTime;
    ECode           ec;

    ec = g_startMutex.Lock();
    assert(NOERROR == ec);

    ec = DzGetCurrentThread(&pICurrentThread);
    assert(NOERROR == ec);

    ec = pICurrentThread->GetStartTime(&subStartTime);
    assert(NOERROR == ec);

    assert(subStartTime.seconds == g_startTime.seconds);
    assert(subStartTime.microseconds == g_startTime.microseconds);

    g_startMutex.Unlock();

    pICurrentThread->Release();

    return NOERROR;
}

EXTERN void GetThreadStartTime()
{
    IThread     *pIThread;
    SystemTime      mainStartTime;
    ECode       ec;

    ec = g_startMutex.Lock();
    assert(NOERROR == ec);

    ec = DzCreateKernelThread(StartTimeThread, (void *)0,
                    0, (ThreadPriorityRank)0xFFFFEEEE, &pIThread);
    if (FAILED(ec)) {
        kprintf("Create thread failed, the ec = 0x%08x\n", ec);

        return;
    }

    ec = pIThread->GetStartTime(&g_startTime);
    assert(NOERROR == ec);

    g_startMutex.Unlock();

    pIThread->Join(INFINITE);

    ec = pIThread->GetStartTime(&mainStartTime);
    assert(NOERROR == ec);

    assert(mainStartTime.seconds == g_startTime.seconds);
    assert(mainStartTime.microseconds == g_startTime.microseconds);

    pIThread->Release();
}
